package prolog.fluents


import scala.collections.jcl.ArrayList


import prolog.terms.Prog
import prolog.terms.Source
import prolog.terms.Term


 /*An Infinite Source.
  If based on a finite Source, it moves to its the first element after reaching its last element.
  A SourceLoop returns 'no' if the original Source is empty. 
  In case the original Source is infinite, a SourceLoop will 
  return the same elements as the original Source. 
  (In particular, this happens if the original Source is also a Source loop).
*/

class SourceLoop(v:ArrayList,s:Source,var i:int) extends Source
			{
			object s:Source
			var v:ArrayList
			var i:int
			def SourceLoop(s:Source,p:Prog){
			super(p)
			this.s=s
			this.v=new ArrayList()
				}


			final def getMemoized():Term={
				 if(null==v || v.size<=0)
				  null;
				  Term T=(Term)v.get(i)
				  i=(i+1)%v.size()
				   T
				  }

			def getElement():Term={
					Term T=null
					if(null!=s)
					{
					T=s.getElement()
					if(null!=T)
					{
					v.add(T)
					}
					else
					s=null
					if(null==s)
					T=getMemoized
					}

			def stop():Unit{v=null;s=null}
			}

